package com.example.io;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author readpage
 * @create 2022-09-18 23:06
 * Person需要满足如下的要求，方可序列化
 * 1.需要实现接口: Serializable
 * 2.当前类提供一个全局常量: serialVersionUID
 * 3.处理当前Person类需要实现Serializable
 */
public class Person implements Comparable, Serializable {
    private static final long serialVersionUID = 1963728865996991037L;
    private String name;
    private static int id;
    private transient int age;
    private Account account;

    public Person() {
        super();
    }

    public Person(String name, int id, int age, Account account) {
        this.name = name;
        this.id = id;
        this.age = age;
        this.account = account;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                ", account=" + account +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        Person person = (Person) o;
        return getAge() == person.getAge() && Objects.equals(getName(), person.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }

    // 按照姓名从小到大排列
    @Override
    public int compareTo(Object o) {
        if (o instanceof Person) {
            Person person = (Person) o;
            int compare = this.name.compareTo(person.name);
            if (compare != 0) {
                return compare;
            } else {
                return Integer.compare(this.age, person.age);
            }
        } else {
            throw new RuntimeException("输入的类型不匹配");
        }
    }
}

class Account implements Serializable {
    private static final long serialVersionUID = -7716710401530367428L;
    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "balance=" + balance +
                '}';
    }
}